﻿using System;
using System.Collections.Generic;
using System.IO;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace Common {

    public class BmpReader {
        public static int loadCustomBmp(string path) {
            Console.WriteLine("reading image {0}", path);
            byte[] header = new byte[54];
            int dataPos;
            int imageSize;
            int width, height;
            byte[] data;
            var stream = File.Open(path, FileMode.Open);
            using (stream) {
                readHeader(header, out dataPos, out imageSize, out width, out height, stream);
                if (imageSize == 0) {
                    imageSize = width * height * 3;
                }
                if (dataPos == 0) {
                    dataPos = 54;
                }
                data = new byte[imageSize];
                stream.Read(data, 0, data.Length);

                int textureId = GL.GenTexture();
                GL.BindTexture(TextureTarget.Texture2D, textureId);
                GL.TexImage2D<byte>(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, width, height, 0, PixelFormat.Bgr, PixelType.UnsignedByte, data);

                //int glRepeat = 0x2901;
                //int glLinear = 0x2601;
                //int glLinearMipmapLinear = 0x2703;
                //GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, ref glRepeat);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
                //GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, ref glRepeat);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
                //GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, ref glLinear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,(int)TextureMagFilter.Linear);
                //GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, ref glLinearMipmapLinear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);

                return textureId;
            }
        }

        private static void readHeader(byte[] header, out int dataPos, out int imageSize, out int width, out int height, FileStream stream) {
            if (stream.Length <= 54) {
                throw new Exception("not a correct bmp file");
            }
            stream.Read(header, 0, header.Length);
            if (System.Text.Encoding.ASCII.GetString(header, 0, 2) != "BM") {
                throw new Exception("not a correct BMP file");
            }
            if (header[0x1e] != 0 | header[0x1c] != 24) {
                throw new Exception("not a correct BMP file");
            }
            dataPos = GLUtility.getIntFromPos(header, 0x0a);
            imageSize = GLUtility.getIntFromPos(header, 0x22);
            width = GLUtility.getIntFromPos(header, 0x12);
            height = GLUtility.getIntFromPos(header, 0x16);

        }
    }
}
